package com.ruoyi.framework.websocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    private final AdvancedWebSocketHandler webSocketHandler;

    // 注入自定义处理器
    public WebSocketConfig(AdvancedWebSocketHandler webSocketHandler) {
        this.webSocketHandler = webSocketHandler;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册处理器并配置端点
        registry.addHandler(webSocketHandler, "/socket")
//                .addInterceptors(new UserHandshakeInterceptor()) // 添加用户认证拦截器
                .setAllowedOriginPatterns("*"); // 允许跨域（生产环境需限制）
    }
}
